package com.example.chasshelper;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.model.Test;
import com.example.chasshelper.utils.DaoUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CreateTestActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = CreateTestActivity.class.getSimpleName();
    private final Activity mContext = CreateTestActivity.this;
    //控件
    private ImageView iv_back;
    private ImageView iv_save;
    private RadioGroup rg_type;
    private RadioButton rb_judge;
    private RadioButton rb_singleChoice;
    private RadioButton rb_multipleChoice;
    private RadioGroup rg_number;
    private RadioButton rb_two;
    private RadioButton rb_three;
    private RadioButton rb_four;
    private RadioButton rb_five;
    private EditText et_context;
    private EditText et_item1;
    private EditText et_item2;
    private EditText et_item3;
    private EditText et_item4;
    private EditText et_item5;
    private EditText et_answer;

    private RadioButton rb_type_current;
    private RadioButton rb_number_current;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //数据
    private Teacher teacher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_test);
        //初始化界面
        initUI();
        //初始化数据
        initData();
    }

    private void initUI() {
        //绑定控件
        iv_back = findViewById(R.id.iv_back);
        iv_save = findViewById(R.id.iv_save);
        rg_type = findViewById(R.id.rg_type);
        rb_judge = findViewById(R.id.rb_judge);
        rb_singleChoice = findViewById(R.id.rb_singleChoice);
        rb_multipleChoice = findViewById(R.id.rb_multipleChoice);
        rg_number = findViewById(R.id.rg_number);
        rb_two = findViewById(R.id.rb_two);
        rb_three = findViewById(R.id.rb_three);
        rb_four = findViewById(R.id.rb_four);
        rb_five = findViewById(R.id.rb_five);
        et_context = findViewById(R.id.et_context);
        et_item1 = findViewById(R.id.et_item1);
        et_item2 = findViewById(R.id.et_item2);
        et_item3 = findViewById(R.id.et_item3);
        et_item4 = findViewById(R.id.et_item4);
        et_item5 = findViewById(R.id.et_item5);
        et_answer = findViewById(R.id.et_answer);
        //点击事件
        iv_back.setOnClickListener(this);
        iv_save.setOnClickListener(this);
    }

    private void initData() {
        teacher = (Teacher) getIntent().getSerializableExtra("teacher");
    }


    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        String context = et_context.getText().toString();
        String item1 = et_item1.getText().toString();
        String item2 = et_item2.getText().toString();
        String item3 = et_item3.getText().toString();
        String item4 = et_item4.getText().toString();
        String item5 = et_item5.getText().toString();
        String answer = et_answer.getText().toString().toUpperCase();//转换为大写
        //获取题目类型
        String type ="未选中";
        String number ="未选中";
        if (rg_type.getCheckedRadioButtonId() != -1) {
            rb_type_current = findViewById(rg_type.getCheckedRadioButtonId());
            type = rb_type_current.getText().toString();
        }
        if (rg_number.getCheckedRadioButtonId() != -1) {
            rb_number_current = findViewById(rg_number.getCheckedRadioButtonId());
            number = rb_number_current.getText().toString();
        }
        switch (view.getId()) {
            case R.id.iv_save://创建题目
                if (type.equals("未选中")) {
                    Toast.makeText(mContext,"请选择题目类型",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (number.equals("未选中")) {
                    Toast.makeText(mContext,"请选择选项个数",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (context.trim().length() == 0 || answer.trim().length() == 0 || item1.trim().length() == 0 || item2.trim().length() == 0) {
                    Toast.makeText(mContext,"题目内容、参考答案、选项1、选项2不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Integer.valueOf(number) >=3 && item3.trim().length() == 0) {
                    Toast.makeText(mContext,"选项3不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Integer.valueOf(number) >=4 && item4.trim().length() == 0) {
                    Toast.makeText(mContext,"选项4不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Integer.valueOf(number) == 5 && item5.trim().length() == 0) {
                    Toast.makeText(mContext, "选项5不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!answer.matches("[a-eA-E]+")) {//答案出现非字母
                    Toast.makeText(mContext, "答案范围应在A-E之间", Toast.LENGTH_SHORT).show();
                    return;
                }
                //答案与选项个数
                if (Integer.valueOf(number) < 5 && answer.contains("E")) {
                    Toast.makeText(mContext, "E选项为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Integer.valueOf(number) < 4 && answer.contains("D")) {
                    Toast.makeText(mContext, "D选项为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (Integer.valueOf(number) < 3 && answer.contains("C")) {
                    Toast.makeText(mContext, "C选项为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                //类型与选项个数
                if (type.equals("判断") && Integer.valueOf(number) != 2) {
                    Toast.makeText(mContext, "判断题只能有两个选项", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (type.equals("单选") && Integer.valueOf(number) <3) {
                    Toast.makeText(mContext, "单选题最少三个选项", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (type.equals("多选") && Integer.valueOf(number) <3) {
                    Toast.makeText(mContext, "多选题最少三个选项", Toast.LENGTH_SHORT).show();
                    return;
                }
                //类型与答案个数
                if (type.equals("判断") && answer.trim().length() !=1) {
                    Toast.makeText(mContext, "判断题只能有一个答案", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (type.equals("单选") && answer.trim().length() != 1) {
                    Toast.makeText(mContext, "单选题只能有一个答案", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (type.equals("多选") && answer.trim().length() == 1) {
                    Toast.makeText(mContext, "多选题最少两个答案", Toast.LENGTH_SHORT).show();
                    return;
                }
                //成功
                Test test = new Test();
                test.setTestContext(context);
                test.setTestAnswer(answer);
                test.setTestType(type);
                test.setTestItemNum(number);
                test.setTestItem1(item1);
                test.setTestItem2(item2);
                test.setTestItem3(item3);
                test.setTestItem4(item4);
                test.setTestItem5(item5);
                testDao.insert(test,teacher);
                Toast.makeText(mContext,"创建题目成功",Toast.LENGTH_SHORT).show();
                finish();
                break;
            case R.id.iv_back://返回
                finish();
                break;
        }
    }

}